﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmOnShelves : Form
    {
        public FrmOnShelves()
        {
            InitializeComponent();
        }

        private static Dictionary<string, LocationGoodsInfo> _dictLocationGoods = null;
        public static Dictionary<string, LocationGoodsInfo> dictLocationGoods
        {
            get
            {
                if (_dictLocationGoods == null) _dictLocationGoods = new Dictionary<string, LocationGoodsInfo>();
                return _dictLocationGoods;
            }
        }

        private static Dictionary<string, LocationGoodsInfo> _dictLocation = null;
        public static Dictionary<string, LocationGoodsInfo> dictLocation
        {
            get
            {
                if (_dictLocation == null) _dictLocation = new Dictionary<string, LocationGoodsInfo>();
                return _dictLocation;
            }
        }

        private static List<LocationGoodsInfo> _lstLocationGoods = null;
        public static List<LocationGoodsInfo> lstLocationGoods
        {
            get
            {
                if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
                return _lstLocationGoods;
            }
        }

        private static int iSort = 0;

        private void FrmOnShelves_Load(object sender, EventArgs e)
        { 
            txtBarCode.Focus();
        }
        //显示扫描行
        private void SelectionBright()
        {
            //if (lstLocationGoods == null || lstLocationGoods.Count <= 0) return;
            ////设置已扫描的为加亮
            //List<LocationGoodsInfo> _LocationGoods = lstLocationGoods.FindAll(o => o.Selected);
            //foreach (var _item in _LocationGoods)
            //{
            //    this.dbGridOnShelves.Select(lstLocationGoods.IndexOf(_item));

            //    this.dbGridOnShelves.SelectionBackColor = Color.Green;
            //}
            if (lstLocationGoods == null || lstLocationGoods.Count <= 0) return;
            string barNo=txtBarCode.Text.Trim();
            if (string.IsNullOrEmpty(barNo)) return;
            if (barNo.Length == 18 || barNo.Length == 17 || barNo.Length == 20)
            { 
                int curIndex = lstLocationGoods.FindIndex(x => x.GOODSBATCHCODE == barNo);
                dbGridOnShelves.Select(curIndex);
                this.dbGridOnShelves.SelectionBackColor = Color.Green;
            }
            
        }
        //扫描排序
        private void Sort(LocationGoodsInfo locationGoods)
        {
            locationGoods.Sort = -1;
            lstLocationGoods.Sort((x, y) =>
            {
                return x.Sort.CompareTo(y.Sort);
            });
            this.dbGridOnShelves.Refresh();

            locationGoods.Sort = iSort++;
        }
        //商品批次条码回车操作
        private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            { 
                string _barCode = txtBarCode.Text.Trim();
                if (string.IsNullOrEmpty(_barCode))
                {
                    MessageBox.Show("请扫描商品条码！"); 
                    return;
                }
                 
                Cursor.Current = Cursors.WaitCursor;

                if (_barCode.Length == 18) //处理商品
                {
                    string strs0 = '0'.ToString();
                    if(!_barCode.StartsWith(strs0))
                    {
                         MessageBox.Show("请扫描正确的上架0条码！");
                         txtBarCode.SelectAll();
                         return;
                    }
                }
                else if(_barCode.Length==17 || _barCode.Length==20)
                { 
                    if(!_barCode.StartsWith("WOC"))
                    {
                         MessageBox.Show("请扫描正确的上架WOC条码！");
                         txtBarCode.SelectAll();
                         return;
                    }
                }
                else  //处理货位
                {
                    MessageBox.Show("请扫描正确的上架条码！");
                    txtBarCode.SelectAll();
                    return;

                }
                    LocationGoodsInfo lgitem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == _barCode);
                    if (lgitem!=null)
                    {
                        //LocationGoodsInfo _LocationGoods = dictLocationGoods[_barCode];
                        lgitem.QTY += 1;

                        //排序
                        Sort(lgitem);
                        //设置已扫描的为加亮
                        //SelectionBright();
                    }
                    else
                    {
                        string _Code = _barCode.Substring(0, _barCode.Length - 8);
                        LocationGoodsInfo _LocationGoods = Duty.OnShelvesUIRule.NewInstance.GetLocationGoodsInfo(_Code);

                        if (_LocationGoods != null)
                        {
                            _LocationGoods.QTY += 1;
                            _LocationGoods.GOODSBATCHCODE = _barCode;
                            //_LocationGoods.Selected = false;
                            lstLocationGoods.Add(_LocationGoods);
                            //dictLocationGoods.Add(_barCode, _LocationGoods);

                            //if (!dictLocation.Keys.Contains(_LocationGoods.LOCATIONCODE))
                            //{
                            //    dictLocation.Add(_LocationGoods.LOCATIONCODE, _LocationGoods);
                            //}

                            //排序
                            Sort(_LocationGoods);
                            //设置已扫描的为加亮
                            //SelectionBright();
                        }
                        else
                        {
                            MessageBox.Show("没有找到匹配的数据！");
                            txtBarCode.SelectAll();
                        }
                    }

                    this.OnShelvesSource.DataSource = null;
                    this.OnShelvesSource.DataSource = lstLocationGoods;
                    this.dbGridOnShelves.DataSource = this.OnShelvesSource;
                    this.dbGridOnShelves.Refresh();
                    SelectionBright();
                    
                    txtQty.Focus();
                    txtQty.SelectAll(); 
            }
            catch (Exception ex)
            {
                txtBarCode.Focus();
                txtBarCode.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {  
                Cursor.Current = Cursors.Default;
            }
        }
        //提交操作
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lstLocationGoods != null && lstLocationGoods.Count > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;

                    List<LocationGoodsInfo> lgItem0 = lstLocationGoods.FindAll(x => x.QTY == 0);
                    if (lgItem0.Count > 0)
                    {
                        MessageBoxEx.Show(string.Format("有{0}条数据的下架数量等于0，请检查再提交？", lgItem0.Count));
                        return;
                    }
                    foreach (var item in lstLocationGoods)
                    {
                        if (item.QTY == 0)
                            lstLocationGoods.Remove(item);
                    }

                    var re = Duty.OnShelvesUIRule.NewInstance.SetOnShelves(LoginBean.Instance.USERCODE, lstLocationGoods);

                    if (re.Params)
                    {
                        lstLocationGoods.Clear();
                        dictLocationGoods.Clear();
                        dictLocation.Clear();
                        this.OnShelvesSource.DataSource = null;
                        this.dbGridOnShelves.DataSource = this.OnShelvesSource;
                        this.dbGridOnShelves.Refresh();

                        txtBarCode.Text = string.Empty;
                        txtQty.Text = string.Empty;
                        txtBarCode.Focus();
                        MessageBox.Show("提交成功！");
                    }
                    else
                    {
                        MessageBox.Show("提交失败!\n" + re.Message);
                        return;
                    }
                }
                else
                {
                    MessageBoxEx.Show("请扫描上架数据再提交！");
                    return; 
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //回车数量操作
        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            string _txtQty = this.txtQty.Text.Trim();

            if (!string.IsNullOrEmpty(_txtQty))
            {
                decimal _Qty = 0;
                try
                {
                    _Qty = Convert.ToDecimal(_txtQty);
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    this.txtQty.Text = string.Empty;
                    this.txtQty.Focus();
                    return;
                }
                if (_Qty == 0)
                {
                    MessageBox.Show("下架数量不能为0");
                    return;
                }
                
                //修改实拣量
                if (lstLocationGoods.Count==0)
                {
                    MessageBox.Show("请选中需改变数量的数据行！");
                    return;
                }
                LocationGoodsInfo _LocationGoods = lstLocationGoods[dbGridOnShelves.CurrentCell.RowNumber];
                _LocationGoods.QTY = _Qty;

                this.OnShelvesSource.DataSource = lstLocationGoods;
                this.dbGridOnShelves.DataSource = this.OnShelvesSource;
                this.dbGridOnShelves.Refresh();
                //设置已扫描的为加亮
                SelectionBright();

                this.txtQty.Text = string.Empty;
                this.txtBarCode.Text = string.Empty;
                this.txtLocation.Text = string.Empty;
                this.txtBarCode.Focus(); 
            }
        }
        //关闭窗体
        private void FrmOnShelves_Closed(object sender, EventArgs e)
        {
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show"); 
            //mi.Invoke(o, null);
            if (lstLocationGoods != null && lstLocationGoods.Count > 0)
            {
                if (MessageBoxEx.Question("退出是否清空当前数据？") == DialogResult.Yes)
                {
                    lstLocationGoods.Clear();
                    dictLocationGoods.Clear();
                    dictLocation.Clear();
                }
            }
            this.Close();
            this.Dispose();
        }
        //关闭窗体提示
        private void FrmOnShelves_Closing(object sender, CancelEventArgs e)
        {
            if (MessageBoxEx.Question("将退出该窗体，确认是否退出？") == DialogResult.No)
                e.Cancel = true;
            
        }
        //双击删除操作
        private void dbGridOnShelves_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dbGridOnShelves.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string gbCode = dbGridOnShelves[curIndex, 4].ToString();
                LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == gbCode);
                string ltCode = curItem.LOCATIONCODE;
                string gbName = curItem.GOODSNAME;

                string Message = string.Format("货位:{0}, 名称:{1}, 商品批次号:{2}, \n 是否删除该数据？", ltCode, gbName, gbCode);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstLocationGoods.Remove(curItem);
                    dictLocationGoods.Remove(gbCode);
                    this.OnShelvesSource.DataSource = null;
                    this.OnShelvesSource.DataSource = lstLocationGoods;
                    this.dbGridOnShelves.DataSource = this.OnShelvesSource;
                    this.dbGridOnShelves.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtBarCode.Focus();
                txtBarCode.SelectAll();
            }


        }
        //回车货位操作
        private void txtLocation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {
                string LtCode = txtLocation.Text.Trim();
                if (string.IsNullOrEmpty(LtCode))
                {
                    MessageBox.Show("请扫架位条码！");
                    return;
                }
                string _barCode = txtBarCode.Text.Trim();
                if (string.IsNullOrEmpty(_barCode))
                {
                    MessageBox.Show("请先扫描商品条码！");
                    return;
                }
                LocationGoodsInfo lgItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == _barCode);

                if (lgItem != null)
                {
                    if (lgItem.LOCATIONCODE != LtCode)
                    {
                        MessageBox.Show("扫描货位与系统记录货位不一致！");
                        txtLocation.SelectAll();
                        return;
                    }
                } 
                txtQty.Focus(); 
                txtQty.SelectAll(); 
            }
            catch (Exception ex)
            {
                txtLocation.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            } 
        }
        //单击选择
        private void dbGridOnShelves_Click(object sender, EventArgs e)
        {
            int curIndex = dbGridOnShelves.CurrentRowIndex;
            if (curIndex < 0) return;
            string gbCode = dbGridOnShelves[curIndex, 4].ToString();
            LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == gbCode);
            txtBarCode.Text = gbCode;
            txtLocation.Text = curItem.LOCATIONCODE;
            txtQty.Text = curItem.QTY.ToString();
            txtQty.Focus();
            txtQty.SelectAll();
            SelectionBright();
        }

    }
}